package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.SuccessorTDao;
import com.sykj.why.mongo.document.entity.RelevantCertificateT;
import com.sykj.why.mongo.document.entity.SuccessorT;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.AuditStatusEnum;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.RelevantCertificateTService;
import com.sykj.why.mongo.service.SuccessorTService;
import com.sykj.why.mongo.vo.entity.SuccessorTVo;
import com.sykj.why.util.BaseTools;
import com.sykj.why.util.DateTool;
import com.sykj.why.util.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eleven
 * @date 2019/6/12
 * @desc 传承人业务实现层
 */

@Service("SuccessorTService")
public class SuccessorTServiceImpl extends BasicServiceImpl<SuccessorT> implements SuccessorTService {

    @Autowired
    private SuccessorTDao successorTDao;

    @Autowired
    private RelevantCertificateTService relevantCertificateTService;

    /**
     * 添加传承人
     * @param successorT
     */
    @Override
    public void saveSuccessor(SuccessorT successorT) {
        //排序
        long count = successorTDao.count(SuccessorT.class, false);
        successorT.setSort((int) count);
        SuccessorTVo successorTVo = findByUser(successorT.getUserId());
        if (successorTVo.getId()!=null){
            throw new WhyException("请勿重复申请");
        }
        //默认未审核状态
        successorT.setAuditStatus(AuditStatusEnum.ApplyStatus.PENDING_REVIEW.getState());
        //设置时间
        successorT.setCreateTime(DateTool.getSystemTime());
        successorT.setUpdateTime(DateTool.getSystemTime());
        //保存
        successorTDao.save(successorT);
    }

    /**
     * 删除传承人
     * @param id
     */
    @Override
    public void removeSuccessor(String id) {
        //通过Id查询
        SuccessorT successorT = findById(id);
        //逻辑删除
        successorTDao.remove(successorT);
    }

    /**
     * 修改传承人
     * @param successorT
     */
    @Override
    public void updateSuccessor(SuccessorT successorT) {
        //通过id查询
        SuccessorT successor = findById(successorT.getId());
        //重新赋值
        successor.setIntro(successorT.getIntro());
        successor.setJob(successorT.getJob());
        successor.setCompany(successorT.getCompany());
        successor.setArtCharacteristics(successorT.getArtCharacteristics());
        successor.setAchievement(successorT.getAchievement());
        successor.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        successorTDao.saveOrupdate(successor);
    }

    /**
     * 启用/禁用传承人
     * @param id
     * @param flag
     */
    @Override
    public void disableOrEnable(String id, boolean flag) {
        //通过id查询
        SuccessorT successorT= findById(id);
        //修改状态
        successorTDao.disableOrEnable(successorT,flag);
    }

    /**
     * 通过id查询传承人
     * @param id
     * @return
     */
    @Override
    public SuccessorT findById(String id) {
        //通过id查询未删除的记录
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        SuccessorT successorT = successorTDao.findOne(query,SuccessorT.class);
        if (successorT!=null){
            return successorT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 通过id查询传承人
     * @param id
     * @return
     */
    @Override
    public SuccessorT findBySuccessorId(String id) {
        //通过id查询未删除的记录
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        SuccessorT successorT = successorTDao.findOne(query,SuccessorT.class);
        return successorT;
    }

    /**
     * 查询所有传承人
     * @return
     */
    @Override
    public List<SuccessorT> findAll() {
        //查询未删除和未禁用的所有记录
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("enable").is(true));
        List<SuccessorT> list = successorTDao.findAll(query,SuccessorT.class);
        return list;
    }

    /**
     * 通过用户查询传承人
     * @param userId
     * @return
     */
    @Override
    public SuccessorTVo findByUser(String userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("enable").is(true).and("userId").is(userId));
        SuccessorT successorT = successorTDao.findOne(query,SuccessorT.class);
        SuccessorTVo vo = new SuccessorTVo();
        if (successorT!=null){
            vo = setVo(successorT,vo);
        }
        return vo;
    }

    /**
     * 分页查询传承人
     * @param pageDTO
     * @return
     */
    @Override
    public Page findByPage(PageDTO pageDTO) {
        //查询未删除的传承人，并分页传给视图
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false));
        long count = successorTDao.count(query, SuccessorT.class);
        List<SuccessorTVo> list = new ArrayList<>();
        for (SuccessorT successorT : successorTDao.findByPage(query, pageDTO, SuccessorT.class)) {
            SuccessorTVo vo = new SuccessorTVo();
            vo = setVo(successorT,vo);
            list.add(vo);
        }
        return new Page(pageDTO, count, list);
    }

    /**
     * 查询待审核传承人
     *
     * @param pageDTO
     * @return
     */
    @Override
    public Page findCheckByPage(PageDTO pageDTO) {
        Query query = new Query(Criteria.where("removed").is(false).
                and("AuditStatus").is(AuditStatusEnum.ApplyStatus.PENDING_REVIEW.getState()));
        long count = successorTDao.count(query, SuccessorT.class);
        List<SuccessorT> list = successorTDao.findByPage(
                query.with(new Sort(Sort.Direction.DESC, "updateTime", "createTime")),
                pageDTO, SuccessorT.class);
        List<SuccessorTVo> voList = new ArrayList<>();
        if (count > 0) {
            for (SuccessorT successorT : list) {
                SuccessorTVo vo = new SuccessorTVo();
                vo = setVo(successorT,vo);
                voList.add(vo);
            }
        }
        return new Page(pageDTO, count, voList);
    }

    public SuccessorTVo setVo(SuccessorT successorT,SuccessorTVo vo){
        BeanUtils.copyProperties(successorT, vo);
        if(successorT.getUserId()!=null){
            List<RelevantCertificateT> fileList = relevantCertificateTService.findByUser(successorT.getUserId());
            vo.setRelevantCertificateList(fileList);
        }
        vo.setStatusName(AuditStatusEnum.ApplyStatus.getName(successorT.getAuditStatus()));
        vo.setCreateTime(BaseTools.longToString(successorT.getCreateTime()));
        vo.setUpdateTime(BaseTools.longToString(successorT.getUpdateTime()));
        return vo;
    }

    /**
     * 传承人审核
     *
     * @param id
     * @param flag
     * @param checkInfo
     */
    @Override
    public void check(String id, boolean flag, String checkInfo) {
        if (StringUtils.isBlank(id)) {
            throw new WhyException(ResultEnum.IDISNULL);
        }
        SuccessorT successorT = successorTDao.get(SuccessorT.class, id, false);
        if (successorT == null) {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
        if (flag) {
            // 审核通过
            successorT.setAuditStatus(AuditStatusEnum.ApplyStatus.PASS.getState());
        } else {
            // 审核不通过
            successorT.setAuditStatus(AuditStatusEnum.ApplyStatus.NO_PASS.getState());
            successorT.setReason(checkInfo);
        }
        successorTDao.saveOrupdate(successorT);
    }

}
